<template>
  <div v-loading="loading" class="AddOrUpdate_main add_container column_three">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="任务单号" prop="code">
          <CodeRuleInput :band-value.sync="formData.code" table-name="production_task" column-name="code" :is-edit="id" />
        </el-form-item>
        <el-form-item label="任务类型" prop="taskTypeCode">
          <el-select v-model="formData.taskTypeCode" placeholder="请选择" :disabled="isPlanAutomatic">
            <el-option
              v-for="item in taskTypeCodelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <!-- <DictSelect :band-value.sync="formData.taskTypeCode" dict-type="taskTypeCode" /> -->
        </el-form-item>

        <el-form-item v-if="canSelectPlan" label="计划单号" class="clickChoose">
          <el-input v-if="canSelectPlan" v-model="formData.planProductionCode" placeholder="请选择" readonly />
          <a v-if="canSelectPlan" class="clickChoose_button" @click.prevent="choosePlanCode">
            <svg-icon icon-class="选择弹框" />
          </a>
          <!-- <el-input v-if="!canSelectPlan" v-model="formData.planProductionCode" placeholder="暂无" readonly disabled />
          <a v-if="!canSelectPlan" class="clickChoose_button">
            <svg-icon icon-class="选择弹框" />
          </a> -->
        </el-form-item>
        <el-form-item v-if="!canSelectPlan" label="计划单号">
          <!-- <el-input v-if="canSelectPlan" v-model="formData.planProductionCode" placeholder="请选择" readonly />
          <a v-if="canSelectPlan" class="clickChoose_button" @click.prevent="choosePlanCode">
            <svg-icon icon-class="选择弹框" />
          </a> -->
          <el-input v-if="!canSelectPlan" v-model="formData.planProductionCode" placeholder="暂无" readonly disabled />

        </el-form-item>

        <el-form-item label="物料编码" class="clickChoose" prop="materialCode">
          <el-input v-if="!isPlanAutomatic&&materialCanChoose" v-model="formData.materialCode" placeholder="请选择" readonly />
          <a v-if="!isPlanAutomatic&&materialCanChoose" class="clickChoose_button" @click.prevent="choosematerialCode">
            <svg-icon icon-class="选择弹框" />
          </a>
          <el-input v-else v-model="formData.materialCode" placeholder="暂无" readonly disabled />

        </el-form-item>

        <el-form-item label="物料名称">
          <el-input v-model="formData.materialName" placeholder="暂无" disabled />
        </el-form-item>
        <el-form-item label="规格型号">
          <el-input v-model="formData.materialModel" placeholder="暂无" disabled />
        </el-form-item>
        <!-- <el-form-item label="工艺要求" prop="engineeringRequirementId">
          <el-select v-if="formData.canEdit=='1'" v-model="formData.engineeringRequirementId" placeholder="请选择" @change="getEngineeringFlowInfo">
            <el-option
              v-for="item in engineeringFlowList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-input v-else v-model="formData.engineeringFlowName" placeholder="暂无" disabled />
        </el-form-item> -->
        <el-form-item label="工艺要求" class="clickChoose" prop="engineeringRequirementId">
          <el-input v-if="!formData.planProductionCode&&formData.model!=='1' && formData.materialName && !formData.engineeringBomId&&!formData.engineeringPathId" v-model="formData.engineeringRequirementCodeVersion" placeholder="请选择" readonly />
          <a v-if="!formData.planProductionCode&&formData.model!=='1' && formData.materialName && !formData.engineeringBomId&&!formData.engineeringPathId" class="clickChoose_button" @click.prevent="chooseRequirements">
            <svg-icon icon-class="选择弹框" />
          </a>
          <el-input
            v-else
            v-model="formData.engineeringRequirementCodeVersion"
            placeholder="暂无"
            disabled
            readonly
          />
        </el-form-item>

        <el-form-item label="BOM版本号" prop="engineeringBomId">
          <el-select v-if="formData.materialName&&!formData.engineeringRequirementId" v-model="formData.engineeringBomId" clearable placeholder="请选择" @change="engineeringBomId_change">
            <el-option
              v-for="item in engineeringBomOptions"
              :key="item.engineeringBomId"
              :label="item.engineeringBomVersion"
              :value="item.engineeringBomId"
            />
          </el-select>
          <el-input
            v-else
            placeholder="暂无"
            disabled
            readonly
          />
        </el-form-item>

        <el-form-item label="工艺路线" class="clickChoose" prop="engineeringPathCode">
          <el-input v-if="formData.materialName&&!formData.engineeringRequirementId" v-model="formData.engineeringPathCode" placeholder="请选择" readonly />
          <a v-if="formData.materialName&&!formData.engineeringRequirementId" class="clickChoose_button" @click.prevent="chooseProcessFlow">
            <svg-icon icon-class="选择弹框" />
          </a>
          <el-input
            v-else
            placeholder="暂无"
            disabled
            readonly
          />
        </el-form-item>
        <el-form-item label="工艺路线版本号">
          <!--          <el-input v-if="formData.materialName&&!formData.engineeringRequirementId" v-model="formData.engineeringPathVersion" placeholder="请选择" readonly />-->
          <!--          <a v-if="formData.materialName&&!formData.engineeringRequirementId" class="clickChoose_button" @click.prevent="chooseProcessFlow">-->
          <!--            <svg-icon icon-class="选择弹框" />-->
          <!--          </a>-->
          <el-input
            v-model="formData.engineeringPathVersion"
            placeholder="暂无"
            disabled
            readonly
          />
        </el-form-item>
        <el-form-item label="工序" prop="engineeringProcessId">
          <el-select v-if="formData.engineeringPathId" v-model="formData.engineeringProcessId" clearable placeholder="请选择" @change="selectChange">
            <el-option
              v-for="item in processOptions"
              :key="item.engineeringProcessId"
              :label="item.engineeringProcessName"
              :value="item.engineeringProcessId"
            />
          </el-select>
          <el-input
            v-else
            v-model="formData.engineeringProcessName"
            placeholder="暂无"
            disabled
            readonly
          />
        </el-form-item>
        <el-form-item label="生产数量" prop="productNumber">
          <el-input v-model="formData.productNumber" type="number" placeholder="请输入" :disabled="isPlanAutomatic" />
        </el-form-item>
        <el-form-item label="责任人" class="clickChoose">
          <el-input v-model="formData.productByName" placeholder="请选择" readonly />
          <a class="clickChoose_button" @click.prevent="choosePeople">
            <svg-icon icon-class="选择弹框" />
          </a>
        </el-form-item>
        <el-form-item label="完成日期">
          <el-date-picker v-model="formData.finishDate" type="date" placeholder="选择日期" :editable="false" />
        </el-form-item>
        <el-form-item label="备注" class="double">
          <el-input v-model="formData.remark" type="textarea" placeholder="请输入" maxlength="200" show-word-limit />
        </el-form-item>
        <el-form-item v-if="isShow" label="首件检验">
          <el-switch
            v-model="formData.qualityFirstItemInspectionCheck"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            inactive-color="#999999"
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit('0')">保存</el-button>
      <el-button @click.stop="onSubmit('2')">提交</el-button>
    </div>
  </div>
</template>

<script>
import {
  addOrUpdate,
  newgetInfo,
  getByMaterialId,
  newaddOrUpdate
} from '@/api/ProductionManagement/taskSheet'
import { list, getInfo as getEngineeringFlow } from '@/api/engineeringManage/processFlow'
import choosePlanCode from './choosePlanCode.vue'
import chooseMaterial from './chooseMaterial'
import chooseMaterialDetail from './chooseMaterialDetail'
import chooseRequirements from './chooseRequirements'

import seachUser from './searchUser.vue'
import moment from 'moment'
import { list as BOMList } from '@/api/engineeringManage/BOMManage'
import ChooseProcessFlow from '@/views/engineeringManage/processFlow/chooseProcessFlow.vue'
// import { getProcessInfoByMaterialId } from '@/api/outsourceManage/outsourcePrice'
import { getInfo as getProcessOptions } from '@/api/engineeringManage/processFlow'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    id: {},
    defaultValue: {}
  },
  data() {
    return {
      isFirstLoad: true,
      loading: true,
      isShow: false,
      pageData: {
        pageNum: 1,
        pageSize: 9999,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        status: '1'
      },
      formData: {
        id: '',
        code: '',
        taskTypeCode: '',
        planProductionCode: '',
        planProductDetailId: '',
        planProductionDetailId: '',
        planProductionId: '',
        materialId: '',
        model: '',
        bomDetailId: '',
        materialCode: '',
        materialName: '',
        materialModel: '',
        engineeringRequirementId: '', // 工艺要求id
        engineeringRequirementCode: '', // 工艺要求编码
        engineeringRequirementCodeVersion: '', // 工艺要求编码版本
        engineeringRequirementVersionCode: '', // 工艺要求版本号
        engineeringRequirementMaterialId: '', // 工艺要求明细id
        engineeringProcessId: '', // 工序ID
        engineeringProcessName: '', // 工序名称
        engineeringProcessNameIndex: '', // 工序层级
        engineeringPathMaterialId: '', // 工艺路线下明细表的ID
        canRequisition: 1,
        engineeringBomId: '', // BOM ID
        engineeringBomCode: '', // BOM编码
        engineeringBomVersion: '', // BOM版本号
        engineeringPathId: '', // 工艺路线Id
        engineeringPathCode: '', // 工艺路线
        engineeringPathVersion: '', // 工艺路线版本号
        productBySelf: '', // 标记工序
        // engineeringRequirementId: '',
        // engineeringFlowName: '',
        // engineeringProcessId: '',
        productNumber: '',
        productByName: '',
        productBy: '',
        finishDate: '',
        qualityFirstItemInspectionCheck: '0',
        remark: '',
        canEdit: '0',
        hasRequire: '0',
        isAutomatic: '0',
        engineeringRequireId: ''
      },
      canSelectPlan: true,
      materialCanChoose: true,
      isPlanAutomatic: false,
      taskTypeCodelList: [ // 选择组件数据源
        { value: '0', label: '正常生产' },
        { value: '1', label: '生产返工' }
      ],
      processOptions: [], // 工序的下拉框数据
      engineeringFlowList: [],
      engineeringFlowDetailList: [],
      engineeringBomOptions: '', // BOM选择框
      listData: {},
      peopleData: {},
      rules: {
        materialCode: [
          { required: true, message: '物料不能为空', trigger: 'change' }
        ],
        code: [
          { required: true, message: '任务单号不能为空', trigger: 'blur' }
        ],
        taskTypeCode: [
          { required: true, message: '任务类型不能为空', trigger: 'blur' }
        ],
        engineeringRequirementId: [
          { required: true, message: '工艺要求不能为空', trigger: 'blur' }
        ],
        engineeringProcessId: [
          { required: true, message: '工序不能为空', trigger: 'blur' }
        ],
        engineeringBomId: [
          { required: true, message: 'BOM版本号不能为空', trigger: 'blur' }
        ],
        engineeringPathCode: [
          { required: true, message: '工艺路线不能为空', trigger: 'blur' }
        ],
        engineeringPathVersion: [
          { required: true, message: '工艺路线版本号不能为空', trigger: 'blur' }
        ],
        productNumber: [
          { required: true, message: '生产数量不能为空', trigger: 'blur' }
        ],
        productByName: [
          { required: true, message: '责任人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'formData.materialId'() {
      if (this.isFirstLoad) {
        return
      }
      if (this.formData.materialId) {
        BOMList({
          pageSize: 0,
          status: 1,
          materialId: this.formData.materialId,
          orderByColumn: 'createTime',
          isAsc: 'desc'
        }).then(res => {
          if (res && res.data && res.data.length > 0) {
            res.data.forEach(item => {
              item.engineeringBomId = item.id
              item.engineeringBomCode = item.code
              item.engineeringBomVersion = item.versionCode
            })
            this.engineeringBomOptions = res.data
          } else {
            this.engineeringBomOptions = []
            this.formData.engineeringBomId = ''
            this.formData.engineeringBomCode = ''
            this.formData.engineeringBomVersion = ''
          }
        })
      }
    },
    'formData.engineeringRequirementId'(val) {
      if (this.isFirstLoad) {
        return
      }
      if (val) {
        this.rules.engineeringBomId[0].required = false
        this.rules.engineeringPathCode[0].required = false
        this.rules.engineeringPathVersion[0].required = false
        this.formData.model = '0'
        this.formData.canRequisition = '1'
      } else {
        this.rules.engineeringBomId[0].required = true
        this.rules.engineeringPathCode[0].required = true
        this.rules.engineeringPathVersion[0].required = true
        this.formData.model = ''
      }
    },
    'formData.engineeringBomId'(val) {
      if (this.isFirstLoad) {
        return
      }
      if (val) {
        this.rules.engineeringRequirementId[0].required = false
        this.formData.model = '1'
      } else {
        this.rules.engineeringRequirementId[0].required = true
        this.formData.model = ''
      }
    },
    'formData.engineeringPathCode'(val) {
      if (this.isFirstLoad) {
        return
      }
      if (val) {
        this.rules.engineeringRequirementId[0].required = false
      } else {
        this.rules.engineeringRequirementId[0].required = true
      }
    },
    'formData.model'(val) {
      if (this.isFirstLoad) {
        return
      }
      if (val == '0') {
        this.rules.engineeringRequirementId[0].required = true
      } else if (val == '1') {
        this.rules.engineeringRequirementId[0].required = false
      } else {
        this.rules.engineeringRequirementId[0].required = true
      }
    }
  },
  created() {
    if (!this.id) {
      this.isShow = true
      this.loading = false
      this.$nextTick(() => {
        this.isFirstLoad = false
      })
    } else {
      // 有id就是编辑
      this.getInfo()
      this.formData.id = this.id
    }
  },
  mounted() {
    // this.getList()
    // this.formData.orderName = this.$store.state.user.name
    // this.formData.orderBy = this.$store.state.user.userId
    // this.formData.orderTime = new Date()
    // this.formData.orderGoodsTime = moment(new Date()).format('YYYY-MM-DD')
  },
  methods: {
    // getDictOptions() {
    //   getDictSelects('taxRateCode').then(res => {
    //     this.taxRateCodeList = res.data
    //   })
    // },
    showPeople(data) {
      this.formData.productBy = data.userId
      this.formData.productByName = data.realName
    },
    // getList() {
    //   list(this.pageData).then(res => {
    //     this.engineeringFlowList = res.data
    //   })
    // },
    // getEngineeringFlowInfo(e) {
    //   this.formData.engineeringProcessId = ''
    //   getEngineeringFlow(e).then(res => {
    //     this.engineeringFlowDetailList = res.data.data
    //   })
    // },
    cellClassName(data) {
      if (data.column.property == 'operate') {
        if (data.rowIndex % 2 == 0) {
          // 单数行
          return 'fixed one'
        } else {
          // 双数行
          return 'fixed two'
        }
      }
    },
    rowClassName({ row, rowIndex }) {
      if (rowIndex == this.tableData.length - 1) {
        return 'addRow'
      }
    },
    removeRow(row) {
      this.tableData.splice(this.tableData.indexOf(row), 1)
    },
    // 计划单号
    choosePlanCode() {
      // if (this.formData.materialCode) {
      this.$layer.iframe({
        // 弹框
        content: {
          content: choosePlanCode, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: this.formData.materialId ? this.formData.materialId : '',
            planProductDetailId: this.formData.planProductDetailId ? this.formData.planProductDetailId : ''
          } // props
        },
        area: ['880px', '700px'],
        title: '选择计划单号', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
      // } else {
      //   return this.$message.warning('请先选择物料编码')
      // }
    },
    getInfo() {
      newgetInfo(this.id).then((res) => {
        Object.keys(this.formData).forEach((key) => {
          // 给右边数据赋值
          this.formData[key] = res.data[key]
        })
        this.$nextTick(() => {
          if (this.formData.engineeringRequirementCode && this.formData.engineeringRequirementVersionCode) {
            this.formData.engineeringRequirementCodeVersion = this.formData.engineeringRequirementCode + '/' + this.formData.engineeringRequirementVersionCode
          }
          if (!this.formData.planProductionCode || this.formData.isAutomatic == 1) {
            this.canSelectPlan = false
          }
          // 自动生成计划单不能更改数量，计划单号，物料号
          if (this.formData.isAutomatic == 1) {
            this.isPlanAutomatic = true
          }
          this.isFirstLoad = false

          if (this.formData.engineeringRequirementId) {
            this.rules.engineeringBomId[0].required = false
            this.rules.engineeringPathCode[0].required = false
            this.rules.engineeringPathVersion[0].required = false
          } else {
            this.rules.engineeringRequirementId[0].required = false
          }
        })
        // if (this.formData.engineeringRequirementId) {
        //   getEngineeringFlow(this.formData.engineeringRequirementId).then(res => {
        //     this.engineeringFlowDetailList = res.data.data
        //   })
        // }
        if (this.formData.engineeringPathId) {
          this.getProcessOptions()
        }

        this.isShow = true
        this.loading = false
        // if (this.formData.materialId) {
        //   this.getProcessInfo()
        // }
      })
    },
    choosePeople() {
      this.$layer.iframe({
        // 弹框
        content: {
          content: seachUser, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            userId: this.formData.productBy ? this.formData.productBy : ''
          } // props
        },
        area: ['880px', '700px'],
        title: '选择责任人', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    // 选择物料弹窗
    choosematerialCode() {
      if (!this.formData.planProductDetailId) {
        this.$layer.iframe({
          // 弹框
          content: {
            content: chooseMaterial, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              selectedRow: this.formData
            }
          },
          area: ['1020px', '700px'],
          title: '选择物料', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      } else {
        this.$layer.iframe({
          // 弹框
          content: {
            content: chooseMaterialDetail, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              selectedRow: this.formData,
              planProductDetailId: this.formData.planProductDetailId
            }
          },
          area: ['1020px', '700px'],
          title: '选择物料', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      }
    },
    // 选择工艺要求
    chooseRequirements() {
      if (this.formData.materialCode) {
        this.$layer.iframe({
          // 弹框
          content: {
            content: chooseRequirements, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              engineeringRequirementMaterialId: this.formData.engineeringRequirementMaterialId,
              materialId: this.formData.materialId
            }
          },
          area: ['1020px', '700px'],
          title: '选择工艺要求', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      } else {
        return this.$message.warning('请先选择物料编码')
      }
    },
    // 选择工艺要求
    showRequiremnet(row) {
      if (this.formData.engineeringRequirementId == row.engineeringRequirementId) {
        this.formData.engineeringRequirementCodeVersion = ''
        this.formData.engineeringRequirementId = ''
        this.formData.engineeringRequirementCode = ''
        this.formData.engineeringRequirementVersionCode = ''
        this.formData.engineeringProcessName = ''
        this.formData.engineeringProcessId = ''
        this.formData.engineeringProcessNameIndex = ''
        this.formData.engineeringRequirementMaterialId = ''
        this.formData.productBySelf = ''
        this.formData.productBySelfName = ''
        this.formData.toInspect = ''
        this.formData.toWms = ''
      } else {
        this.formData.engineeringRequirementCodeVersion = row.engineeringRequirementCode + '/' + row.engineeringRequirementVersionCode
        this.formData.engineeringRequirementId = row.engineeringRequirementId
        this.formData.engineeringRequirementCode = row.engineeringRequirementCode
        this.formData.engineeringRequirementVersionCode = row.engineeringRequirementVersionCode
        this.formData.engineeringProcessName = row.engineeringProcessName
        this.formData.engineeringProcessId = row.engineeringProcessId
        this.formData.engineeringProcessNameIndex = row.engineeringProcessNameIndex
        this.formData.engineeringRequirementMaterialId = row.id
        this.formData.productBySelf = row.productBySelf
        this.formData.productBySelfName = row.productBySelf == '1' ? '自己生产' : '外协'
        this.formData.toInspect = row.toInspect
        this.formData.toWms = row.toWms
      }
    },
    // 选择计划单号
    showPlancode(item) {
      console.log('item', item)
      if (this.formData.planProductionId == item.planProductionId && this.formData.materialId == item.materialId) {
        this.formData.planProductionCode = ''
        this.formData.planProductionId = ''
        this.formData.planProductDetailId = ''
        this.formData.planProductionDetailId = ''

        this.formData.model = ''
        this.formData.materialId = ''
        this.formData.materialCode = ''
        this.formData.materialName = ''
        this.formData.materialModel = ''
        this.formData.materialTypeName = ''
        this.formData.materialUnitName = ''
        this.formData.engineeringProcessName = ''
        this.formData.engineeringProcessId = ''
        this.formData.engineeringProcessNameIndex = ''
        this.formData.engineeringPathMaterialId = ''
        this.formData.engineeringRequirementMaterialId = ''
        this.formData.productNumber = ''

        this.formData.engineeringRequirementCodeVersion = ''
        this.formData.engineeringRequirementId = ''
        this.formData.engineeringRequirementCode = ''
        this.formData.engineeringRequirementVersionCode = ''
        this.formData.engineeringProcessName = ''
        this.formData.engineeringProcessId = ''
        this.formData.engineeringProcessNameIndex = ''
        this.formData.engineeringRequirementMaterialId = ''
        this.formData.productBySelf = ''
        this.formData.productBySelfName = ''
        this.formData.toInspect = ''
        this.formData.toWms = ''

        this.formData.engineeringBomId = ''
        this.formData.engineeringBomCode = ''
        this.formData.engineeringBomVersion = ''

        this.formData.engineeringPathId = ''
        this.formData.engineeringPathCode = ''
        this.formData.engineeringPathVersion = ''

        this.formData.finishDate = ''
        this.materialCanChoose = true
        return
      }
      // this.listData = item
      this.formData.planProductionCode = item.planProductionCode
      this.formData.planProductionId = item.planProductionId
      this.formData.planProductDetailId = item.id
      this.formData.planProductionDetailId = item.id
      this.formData.model = item.planProductionModel
      this.formData.finishDate = item.planDealTime

      if (item.planProductionModel == '0') {
        this.formData.engineeringRequirementId = item.engineeringRequirementId
        this.formData.engineeringRequirementCode = item.engineeringRequirementCode
        this.formData.engineeringRequirementVersionCode = item.engineeringRequirementVersionCode
        this.formData.engineeringRequirementCodeVersion = item.engineeringRequirementCode + '/' + item.engineeringRequirementVersionCode

        // 物料工序置空
        this.formData.materialId = ''
        this.formData.materialCode = ''
        this.formData.materialName = ''
        this.formData.materialModel = ''
        this.formData.materialTypeName = ''
        this.formData.materialUnitName = ''
        this.formData.engineeringProcessName = ''
        this.formData.engineeringProcessId = ''
        this.formData.engineeringProcessNameIndex = ''
        this.formData.engineeringPathMaterialId = ''
        this.formData.engineeringRequirementMaterialId = ''
        this.formData.productNumber = ''
        this.materialCanChoose = true
      }

      if (item.planProductionModel == '1') {
        this.formData.materialId = item.materialId
        this.formData.materialCode = item.materialCode
        this.formData.materialName = item.materialName
        this.formData.materialModel = item.materialModel
        this.formData.materialTypeName = item.materialTypeName
        this.formData.materialUnitName = item.materialUnitName

        this.formData.engineeringRequirementCodeVersion = ''
        this.formData.engineeringRequirementId = ''
        this.formData.engineeringRequirementCode = ''
        this.formData.engineeringRequirementVersionCode = ''
        this.formData.engineeringProcessName = ''
        this.formData.engineeringProcessId = ''
        this.formData.engineeringProcessNameIndex = ''
        this.formData.engineeringRequirementMaterialId = ''
        this.formData.productBySelf = ''
        this.formData.productBySelfName = ''
        this.formData.toInspect = ''
        this.formData.toWms = ''

        this.formData.engineeringBomId = item.engineeringBomId
        this.formData.engineeringBomCode = item.engineeringBomCode
        this.formData.engineeringBomVersion = item.engineeringBomVersion

        this.formData.engineeringPathId = item.engineeringPathId
        this.formData.engineeringPathCode = item.engineeringPathCode
        this.formData.engineeringPathVersion = item.engineeringPathVersion
        this.getProcessOptions()
        this.materialCanChoose = false
      }
    },
    // 选择物料确认
    chooseMaterialCallBack(row) {
      if (this.formData.materialId == row.id) {
        this.canSelectPlan = true
        this.formData.materialId = ''
        this.formData.materialCode = ''
        this.formData.materialName = ''
        this.formData.materialModel = ''
        this.formData.materialTypeName = ''
        this.formData.materialUnitName = ''
        this.formData.bomDetailId = ''
      } else {
        // 如果未选择计划单则禁用计划单
        if (!this.formData.planProductionCode) {
          this.canSelectPlan = false
        }
        row.materialId = row.id
        this.formData.materialId = row.id
        this.formData.materialCode = row.materialCode
        this.formData.materialName = row.materialName
        this.formData.materialModel = row.materialModel
        this.formData.materialTypeName = row.materialTypeName
        this.formData.materialUnitName = row.materialUnitName
        this.formData.bomDetailId = row.bomDetailId
        // 获取工艺流程
        // this.getProcessInfo()
      }

      // 工艺要求置空
      this.formData.engineeringRequirementCodeVersion = ''
      this.formData.engineeringRequirementId = ''
      this.formData.engineeringRequirementCode = ''
      this.formData.engineeringRequirementVersionCode = ''
      this.formData.engineeringProcessName = ''
      this.formData.engineeringProcessId = ''
      this.formData.engineeringPathMaterialId = ''
      this.formData.engineeringProcessNameIndex = ''
      this.formData.engineeringRequirementMaterialId = ''
    },
    // 选择计划单物料
    chooseMaterialDetailCallBack(row) {
      // row.materialId = row.id
      this.formData.materialId = row.materialId
      this.formData.materialCode = row.materialCode
      this.formData.materialName = row.materialName
      this.formData.materialModel = row.materialModel
      this.formData.materialTypeName = row.materialTypeName
      this.formData.materialUnitName = row.materialUnitName
      this.formData.engineeringProcessName = row.engineeringProcessName
      this.formData.engineeringProcessId = row.engineeringProcessId
      this.formData.engineeringProcessNameIndex = row.engineeringProcessNameIndex
      this.formData.engineeringRequirementMaterialId = row.id
      this.formData.productNumber = row.needNumber
      this.formData.toInspect = row.toInspect
      this.formData.toWms = row.toWms
      this.formData.productBySelf = row.productBySelf
      this.formData.productBySelfName = row.productBySelfName
    },
    // 提交
    onSubmit(num) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.SETTINGS.reg_no_zero.test(this.formData.productNumber)) {
            this.$message.warning('生产数量不正确')
            return
          }
          this.formData.finishDate = this.formData.finishDate ? moment(this.formData.finishDate).format('YYYY-MM-DD') : ''
          newaddOrUpdate({ ...this.formData, status: num }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.getList() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid) // 关闭弹框
    },

    getProcessInfo() {
      getByMaterialId(this.formData.materialId).then(res => {
        if (res.data) {
          this.formData.engineeringRequirementId = res.data.flowId
          this.engineeringFlowDetailList = res.data.flowCodeData
          this.formData.engineeringFlowName = res.data.flowName
          this.formData.canEdit = '0'
          this.formData.hasRequire = '1'
          this.formData.engineeringRequireId = res.data.id
        } else {
          this.formData.engineeringRequirementId = ''
          this.formData.engineeringProcessId = ''
          this.formData.engineeringFlowName = ''
          this.formData.canEdit = '1'
          this.formData.hasRequire = '0'
          this.getList()
        }
      })
    },

    chooseProcessFlow() {
      this.$layer.iframe({
        // 弹框
        content: {
          content: ChooseProcessFlow, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            selectedRow: this.formData.engineeringPathId
          }
        },
        area: ['880px', '700px'],
        title: '选择工艺路线', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    chooseProcessFlowCallBack(row, index) {
      if (this.formData.engineeringPathId == row.id) {
        this.formData.engineeringPathId = ''
        this.formData.engineeringPathCode = ''
        this.formData.engineeringPathVersion = ''

        this.processOptions = []
        this.formData.engineeringProcessId = ''
        this.formData.engineeringProcessName = ''
        this.formData.engineeringProcessNameIndex = ''
        this.formData.engineeringPathMaterialId = ''
      } else {
        this.formData.engineeringPathId = row.id
        this.formData.engineeringPathCode = row.code
        this.formData.engineeringPathVersion = row.versionCode
        this.getProcessOptions()
      }
    },
    getProcessOptions() {
      getProcessOptions(this.formData.engineeringPathId).then(res => {
        this.processOptions = res.data.data
      })
    },
    selectChange(val) {
      const selectedRow = this.processOptions.filter(item => item.engineeringProcessId == val)[0]
      if (selectedRow) {
        this.formData.engineeringProcessNameIndex = selectedRow.engineeringProcessNameIndex
        this.formData.engineeringProcessName = selectedRow.engineeringProcessName
        this.formData.engineeringPathMaterialId = selectedRow.id
        this.formData.productBySelf = selectedRow.productBySelf
        this.formData.toInspect = selectedRow.toInspect
        this.formData.productBySelfName = selectedRow.productBySelf == '1' ? '自己生产' : '外协'
        // this.formData.canRequisition = selectedRow.engineeringProcessNameIndex == this.processOptions.length ? '1' : '0'
        this.formData.canRequisition = '1'
      }
    },
    engineeringBomId_change(val) {
      if (val) {
        const selectedRow = this.engineeringBomOptions.filter(item => item.engineeringBomId == val)[0]

        this.formData.engineeringBomId = selectedRow.engineeringBomId
        this.formData.engineeringBomCode = selectedRow.engineeringBomCode
        this.formData.engineeringBomVersion = selectedRow.engineeringBomVersion
      } else {
        this.formData.engineeringBomId = ''
        this.formData.engineeringBomCode = ''
        this.formData.engineeringBomVersion = ''
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-form-item--medium {
  .el-form-item__label {
    width: 160px !important;
  }
}
.title {
  padding-left: 20px;
  padding-bottom: 20px;
  font-size: 24px;
  width: 100%;
}

.numberComponents {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

/deep/ .el-input-number.is-controls-right .el-input-number__increase {
  height: 15px;
}

.el-icon-remove {
  font-size: 25px;
  color: red;
  height: 100%;
  width: 100%;
  border: 0px;
  background-color: transparent;
}

/deep/ .el-form-item__content {
  margin-left: 0 !important;
}

/deep/ .el-form-item__label {
  text-align: right;
}

.expand_radio {
  /deep/ .el-form-item__content {
    text-align: left;
  }
}

.expand_container {
  .el-form-item {
    margin-bottom: 10px;
  }

  .expand_file {
    margin-bottom: 0px;
  }

  /deep/ .el-form-item__label {
    width: 130px !important;
  }

  /deep/ .el-form-item__content {
    width: 710px !important;
  }
}

.main_main {
  .form_padding {
    padding: 0 !important;

    /deep/ .el-form-item {
      margin-right: 0px;
    }

    /deep/ .el-form-item__label {
      width: 120px !important;
    }

    /deep/ .el-form-item__content {
      width: 270px;
    }

    .remark {
      /deep/ .el-form-item__content {
        width: 690px !important;
        height: 60px;
        overflow-y: auto;
      }
    }
  }
}

/deep/ .el-table th.el-table__cell>.cell {
  position: unset;
}

.order_header {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;

  .info_button {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>
